﻿using System;
using System.Windows.Forms.Design;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.ComponentModel.Design;
using System.Windows.Forms.Design.Behavior;

namespace GradienLabelComponent
{    
    
    [Designer(typeof(GradientLabelDesign))]
    [DefaultEvent("OnChangeProperties")]
    public partial class GradientLabel : Label
    {
        private Color startColor = Color.LightGreen;
        private Color endColor = Color.DarkGreen;

        private event EventHandler onChangeProperties;

        [Category("Gradient"), Description("Start gradient color"), DefaultValue(typeof(Color), "LightGreen")]
        public Color StartColor
        {
            get { return this.startColor; }
            set { this.startColor = value; ChangeGradientProperties(); }
        }

        [Category("Gradient"), Description("End gradient color"), DefaultValue(typeof(Color), "DarkGreen")]
        public Color EndColor
        {
            get { return this.endColor; }
            set { this.endColor = value; ChangeGradientProperties(); }
        }
        [Category("Gradient"), Description("Calling when one of gradient colors are changed")]
        public event EventHandler OnChangeProperties
        {
            add { this.onChangeProperties += value; }
            remove { this.onChangeProperties -= value; }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Color c1 = Color.FromArgb(100, this.startColor);
            Color c2 = Color.FromArgb(100, this.endColor);

            Brush b = new LinearGradientBrush(ClientRectangle, c1, c2, 10);

            e.Graphics.FillRectangle(b, ClientRectangle);
            b.Dispose();
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            base.OnPaintBackground(pevent);
            if (DesignMode)
            {
                //string text = "Delegate Copyright";
                //pevent.Graphics.DrawString(text, this.Font, new SolidBrush(Color.Black), 0, this.Height - (int)pevent.Graphics.MeasureString(text, this.Font).Height - 3);
            }
        }
        // Overpaint for changing gradient colors
        private void ChangeGradientProperties()
        {
            this.Invalidate();
            if (this.onChangeProperties != null)
            {
                this.onChangeProperties(this, new EventArgs());
            }
        }
        public GradientLabel()
        {
            InitializeComponent();
        }
    }

    /*
     * Design Class
     */
    class GradientLabelDesign : ControlDesigner
    {
        protected bool MouseOver;
        private DesignerActionListCollection actionLists;

        public override DesignerActionListCollection ActionLists
        {
            get 
            {
                if (this.actionLists == null)
                {
                    this.actionLists = new DesignerActionListCollection();
                    this.actionLists.Add(new GradientLabelActionList(this.Component));
                }
                return this.actionLists;
            }
        }
        public override GlyphCollection GetGlyphs(GlyphSelectionType selectionType)
        {
            //return base.GetGlyphs(selectionType);
            GlyphCollection glyph = base.GetGlyphs(selectionType);
            if (selectionType != GlyphSelectionType.NotSelected)
            {
                glyph.Add(new ZoomGlyph(this.BehaviorService, this.Control));
            }
            return glyph;
        }

        protected override void PostFilterProperties(IDictionary properties)
        {
            base.PostFilterProperties(properties);
            properties.Remove("BackColor");
        }

        protected override void OnMouseEnter()
        {
            MouseOver = true;
            base.OnMouseEnter();
            Control.Invalidate();
        }
        protected override void OnMouseLeave()
        {
            MouseOver = false;
            base.OnMouseLeave();
            Control.Invalidate();
        }
        protected override void OnPaintAdornments(PaintEventArgs pe)
        {
            base.OnPaintAdornments(pe);
            if (MouseOver)
            {
                string text = "-----";
                pe.Graphics.DrawString(text, Control.Font, new SolidBrush(Color.Blue), 0, this.Control.Height - (int)pe.Graphics.MeasureString(text, Control.Font).Height - 4);
            }
        }
        public override void DoDefaultAction()
        {
            //base.DoDefaultAction();
            MessageBox.Show("Double Click");
        }

    }
}
